package com.dewod.producesales.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ObjectUtils;
import com.dewod.producesales.R;
import com.dewod.producesales.adapter.DeliveryAdapter;
import com.dewod.producesales.model.request.OrderDelivery;
import com.dewod.producesales.model.response.StoreSalesContract;
import com.dewod.producesales.model.response.base.ResponseData;
import com.dewod.producesales.utils.request.RequestApiUtil;
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.client.utils.DateUtils;

import org.json.JSONException;

import java.util.List;

public class DeliveryDetailActivity extends AppCompatActivity {

    private TextView tvContractNumber, tvManufacturerName, tvEstimatedShippingDate, tvShippingStatus;
//    private EditText etRemark;
    private Button btnSave;

    private StoreSalesContract contract;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delivery_detail);

        // 初始化视图
        initViews();

        // 获取传递过来的数据
        initData();

        // 数据展示
        dataShow();

        // 绑定监听
        initBtnListener();
    }





    private void initData() {
        String contractJson = getIntent().getStringExtra("contract");
        contract = JSON.parseObject(contractJson, StoreSalesContract.class);

        // 控制是否允许编辑
        if (contract.getLogisticsStatus() == 1) { // 已发货
            btnSave.setVisibility(View.GONE); // 隐藏保存按钮
//            etRemark.setEnabled(false); // 不可编辑
        } else {
            btnSave.setVisibility(View.VISIBLE);
//            etRemark.setEnabled(true);
        }
    }

    private void dataShow() {
        tvContractNumber.setText(contract.getSalesContractNo());
        tvManufacturerName.setText( contract.getDeviceSupplier());
        tvEstimatedShippingDate.setText(ObjectUtils.isEmpty( contract.getExpectedShipDate())? "未安排发货时间" : DateUtils.formatDate(contract.getExpectedShipDate(), "yyyy-MM-dd"));
        tvShippingStatus.setText(getDisplayStatus(contract.getLogisticsStatus()));

//        etRemark.setText(contract.getLogisticsInfo());
    }

    private void initViews() {
        tvContractNumber = findViewById(R.id.tv_contract_number);
        tvManufacturerName = findViewById(R.id.tv_manufacturer_name);
        tvEstimatedShippingDate = findViewById(R.id.tv_estimated_shipping_date);
        tvShippingStatus = findViewById(R.id.tv_shipping_status);
//        etRemark = findViewById(R.id.et_remark);
        btnSave = findViewById(R.id.btn_save);
    }

    private void initBtnListener() {
        btnSave.setOnClickListener(v -> {
//            String remark = etRemark.getText().toString();

            RequestApiUtil.updateRemark(new OrderDelivery(contract.getSalesContractNo()), responseData -> {
                this.runOnUiThread(() -> {
                    if(responseData.getSuccess()){
                        Toast.makeText(this, "更新成功 ", Toast.LENGTH_SHORT).show();
                        // 离开当前页面
                        Intent intent = new Intent("ACTION_DATA_UPDATED");
                        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
                        finish();
                    }else {
                        Toast.makeText(this, "更新失败 " + responseData.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
            });


        });
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        getIntent().removeExtra("contract");
    }

    // 状态转换方法
    private String getDisplayStatus(int status) {
        switch (status) {
            case 0:
                return "未发货";
            case 1:
                return "已发货";
            default:
                return "未知状态";
        }
    }

}
